package com.example.cadmin.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cadmin.entity.Host;
import com.example.cadmin.mapper.HostMapper;
import com.example.cadmin.utils.Netty.sendCmd;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 智能选层柜 系统设置-服务实现类
 * </p>
 *
 * @author zzsoft
 * @since 2022-08-26
 */
@Service
public class SettingService {

    @Resource
    public SysParamsetService sysParamsetService;
    @Resource
    public sendCmd sendCmd;
    @Resource
    public HostService hostService;
    @Resource
    public HostMapper hostMapper;


    private String ConvertName(String cmdName) {
        if ("layerDistance".equals(cmdName)) {
            return "LAYERDIS:";
        } else if ("layerPulse".equals(cmdName)) {
            return "LAYERPUL:";
        } else if ("manualSpeed".equals(cmdName)) {
            return "MANSPEED:";
        } else if ("autoSpeed".equals(cmdName)) {
            return "AUTOSPEED:";
        } else {
            return "RSTSPEED:";
        }
    }

    /**
     * 层脉冲设置
     */
    public ResponseModel pulseSubmit(JSONObject data, HttpServletRequest request) {
        sendCmd.send("SETPUL:"+ data.getStr("layer").split("e")[1]+"/"+data.getStr("pulse"),request,null);
        HttpSession session = request.getSession();
        UpdateWrapper<Host> updateWrapper = new UpdateWrapper<Host>().eq("ip", session.getAttribute("cabinet"));
        String layer = null;
        if ("pulse1".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse1", data.getStr("pulse"));
            layer = "1";
        }else if ("pulse2".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse2", data.getStr("pulse"));
            layer = "2";
        }else if ("pulse3".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse3", data.getStr("pulse"));
            layer = "3";
        }else if ("pulse4".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse4", data.getStr("pulse"));
            layer = "4";
        }else if ("pulse5".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse5", data.getStr("pulse"));
            layer = "5";
        }else if ("pulse6".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse6", data.getStr("pulse"));
            layer = "6";
        }else if ("pulse7".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse7", data.getStr("pulse"));
            layer = "7";
        }else if ("pulse8".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse8", data.getStr("pulse"));
            layer = "8";
        }else if ("pulse9".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse9", data.getStr("pulse"));
            layer = "9";
        }else if ("pulse10".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse10", data.getStr("pulse"));
            layer = "10";
        }else if ("pulse11".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse11", data.getStr("pulse"));
            layer = "11";
        }else if ("pulse12".equals(data.getStr("layer"))) {
            updateWrapper.set("pulse12", data.getStr("pulse"));
            layer = "12";
        }
        boolean updateStatus = hostService.update(updateWrapper);
        return updateStatus ? ResponseModel.success(layer + "层脉冲设置成功") : ResponseModel.fail(layer + "层脉冲设置失败");
    }

    /**
     * 温湿度预警设置
     */
    public ResponseModel temperatureAndHumidityWarning(JSONObject data, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UpdateWrapper<Host> updateWrapper = new UpdateWrapper<Host>().eq("number", session.getAttribute("number"));
        if (StrUtil.isNotEmpty(data.getStr("temperatureUpper"))) {
            updateWrapper.set("temperature_upper", data.getStr("temperatureUpper"));
        }
        if (StrUtil.isNotEmpty(data.getStr("humidityUpper"))) {
            updateWrapper.set("humidity_upper", data.getStr("humidityUpper"));
        }
        boolean updateStatus = hostService.update(updateWrapper);
        return updateStatus ? ResponseModel.success("预警设置成功") : ResponseModel.fail("预警设置失败");


    }

    /**
     * 调试模式
     */
    public ResponseModel debugSave(JSONObject data, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UpdateWrapper<Host> updateWrapper = new UpdateWrapper<Host>().eq("number", session.getAttribute("number"));
        updateWrapper.set("debug", data.getStr("debug"));
        boolean updateStatus = hostService.update(updateWrapper);
        sendCmd.send("SETDBG:"+data.getStr("debug"), request,null);
        return updateStatus ? ResponseModel.success("调试模式成功") : ResponseModel.fail("调试模式失败");
    }
    /**
     * RFID实时读取
     */
    public ResponseModel rfidRealtimeReadSave(JSONObject data, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UpdateWrapper<Host> updateWrapper = new UpdateWrapper<Host>().eq("number", session.getAttribute("number"));
        updateWrapper.set("real_time_read", data.getStr("real_time_read"));
        boolean updateStatus = hostService.update(updateWrapper);
        sendCmd.send("SETRFIDREAD:"+data.getStr("real_time_read"), request,null);
        return updateStatus ? ResponseModel.success("RFID实时读取保存成功") : ResponseModel.fail("RFID实时读取保存失败");
    }

    /**
     * 系统 ip表格
     */
    public Map<String, Object> hostIpList(int page, int limit) {
        IPage<Map<String, Object>> hostPage = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
        hostPage = hostMapper.selectMapsPage(hostPage, null);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", hostPage.getTotal());
        map.put("data", hostPage.getRecords());
        return map;
    }

    /**
     * 主机IP信息修改保存
     *
     * @param data
     * @return
     */
    public ResponseModel hostEditSave(JSONObject data) {
        UpdateWrapper<Host> hostUpdateWrapper = new UpdateWrapper<>();
        hostUpdateWrapper.eq("id", data.getStr("id")).set("ip", data.getStr("ip")).set("number", data.getStr("number"));
        boolean status = hostService.update(hostUpdateWrapper);
        return status ? ResponseModel.success("修改主机信息成功") : ResponseModel.fail("修改主机信息失败");
    }

    /**
     * 主机IP信息删除
     *
     * @return
     */
    public ResponseModel hostRemoveId(String id) {
        boolean status = hostService.removeById(id);
        return status ? ResponseModel.success("删除主机信息成功") : ResponseModel.fail("删除主机信息失败");
    }

    /**
     * 保存
     */
    public ResponseModel hostAddSave(JSONObject data) {
        Host host = hostService.getOne(new QueryWrapper<Host>().orderByDesc("id").last("limit 1"));
        host.setId(host.getId() + 1);
        host.setNumber(data.getStr("number"));
        host.setIp(data.getStr("ip"));
        boolean status = hostService.save(host);
        return status ? ResponseModel.success("新增主机信息成功") : ResponseModel.fail("新增主机信息失败");
    }
}
